﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace shopform
{
    public partial class NewProjectForm : Form
    {
        private Company aComp = new Company();
        private CompanyReferent aCompRef = new CompanyReferent();

        public NewProjectForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Builds or rebuilds project id.
        /// </summary>
        private void rebuildProjectID()
        {
            try
            {
                this.aCompRef = new CompanyReferent(this.CBCompanyReferent.SelectedValue.ToString());
                //this.aComp = new Company(this.TBCompanyID.Text.ToString());

                String s = "";
                //CC_COMPNAME_PROJDATE_PROJCITY_PROJNAME
                s = s + Char.ToUpper(this.aComp.CompanyCountry[0]) +
                        Char.ToUpper(this.aComp.CompanyCountry[1]);

                s = s + this.aComp.CompanyName.ToUpper().Replace(" ", "").Replace(".", "");

                s = s + System.DateTime.Now.Date.ToShortDateString().Replace(".", "");

                s = s + this.TBProjectCity.Text.ToUpper().Replace(" ", "");

                s = s + this.TBProjName.Text.ToUpper().Replace(" ","");

                this.TBProjID.Text = s;
            }
            catch (Exception)
            {
                MessageBox.Show("Elobb toltsd ki a mezoket a generalashoz!");
            }
        }

        /// <summary>
        /// Populates combobox after choosing the right company.
        /// </summary>
        private void feltoltCB()
        {
            DataTable dt = CompanyReferent.getAll(this.TBCompanyID.Text);

            this.CBCompanyReferent.DataSource = dt;
            this.CBCompanyReferent.DisplayMember = "CompanyReferentName";
            this.CBCompanyReferent.ValueMember = "CompanyReferentMail";
            this.CBCompanyReferent.SelectedIndex = -1;
        }

        private void ButtonSearchCompany_Click(object sender, EventArgs e)
        {
            try
            {
                String selectedCompany;
                CompanyIDSelectorForm aCompanyIDSelector = new CompanyIDSelectorForm();
                aCompanyIDSelector.ShowDialog();
                selectedCompany = aCompanyIDSelector.getSelectedCompanyID();
                aCompanyIDSelector.Dispose();

                if (selectedCompany != "none")
                {
                    this.TBCompanyID.Text = selectedCompany;
                    this.aComp = new Company(selectedCompany);
                    this.TBCompanyName.Text = this.aComp.CompanyName;
                    this.feltoltCB();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Hiba\n" + ex.Message);
            }
        }

        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                Project aProject = new Project();
                aProject.CompanyID = this.TBCompanyID.Text;
                aProject.CompanyReferentMail = this.CBCompanyReferent.SelectedValue.ToString();
                aProject.ProjectID = this.TBProjID.Text;
                aProject.ProjectAddr = this.TBProjectAddr.Text;
                aProject.ProjectCity = this.TBProjectCity.Text;
                aProject.ProjectName = this.TBProjName.Text;

                aProject.insertProject();
                MessageBox.Show("Projekt letrehozva!");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ButtonGenerateProjID_Click(object sender, EventArgs e)
        {
            this.rebuildProjectID();
        }
    }
}
